package com.ml.mall.api.category;

import com.ml.mall.annotation.LogMethodInvokeInformation;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.page.Page;
import com.ml.mall.bean.req.category.SuggestedCategoryNewDto;
import com.ml.mall.bean.req.category.SuggestedCategoryQueryPageDto;
import com.ml.mall.bean.res.category.SuggestedCategoryQueryPageRes;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.service.category.SysSuggestedCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @description 建议目录接口
 * @author yx
 * @date 2021/9/27
 * @version 1.0
*/
@Api(tags = "【auth】建议目录接口")
@RestController
@RequestMapping("/suggested_category")
@Slf4j
public class SysSuggestedCategoryController {
    @Autowired
    private SysSuggestedCategoryService sysSuggestedCategoryService;

    @PostMapping("/new")
    @ApiOperation("添加建议目录")
    public ApiRestResult suggestedCategoryNew(@Valid @RequestBody SuggestedCategoryNewDto dto){
        log.info("create new category=request====={}", dto);
        Boolean result = sysSuggestedCategoryService.createSuggestedCategory(dto);
        if(!result){
            throw new BizException(CommonEnum.INSERT_ERROR);
        }
        log.info("create new category=response====={}", result);
        return ApiRestResult.ok(result);
    }

    @PostMapping("/query_page")
    @ApiOperation("分页查询建议目录")
    @LogMethodInvokeInformation
    public ApiRestResult suggestedCategoryQueryPage(@Valid @RequestBody SuggestedCategoryQueryPageDto dto){
        log.info("query suggested category by page=request====={}",dto);
        Page<SuggestedCategoryQueryPageRes> result = sysSuggestedCategoryService.queryPage(dto);
        if(null == result){
            throw new BizException(CommonEnum.SELECT_ERROR);
        }
        log.info("query suggested category by page=response====={}",result);
        return ApiRestResult.ok(result);
    }
}
